package proxy 
{
	import capi.saveFile;
	import debug.ASSERT;
	import down.Downloader;
	import nsdecl.ns_fileurl_const;
	import nsdecl.ns_singleton;
	import common.Struct;
	import pool.Pool;
	import utils.XMLProxy;
	/**
	 * proxy模块的对外接口
	 * @author anod
	 */
	public class DataAccess
	{
		use namespace ns_fileurl_const;
		use namespace ns_singleton;
		
		ns_fileurl_const static const DISK_DATA_FILE:String = "data";
		ns_fileurl_const static function get DISK_DATA_URL():String 
		{
			return DISK_DATA_FILE + ".xml";
		}
		
		ns_singleton static var cInstance:DataAccess;
		public static function get instance():DataAccess
		{
			if ( cInstance == null ) 
				cInstance = new DataAccess();
			return cInstance;
		}
		
	///------------------------------------------
		private var mStruct:Struct;
		private var mStaffProxy:StructStaffProxy;
		private var mGroupProxy:StructGroupProxy;
		private var mTimesegProxy:StructTimeProxy;
		
		public function DataAccess() 
		{
			ASSERT( cInstance == null, "DataAccess是单例模式！！" );
			mStruct = new Struct();
		}
		
		public function initial():void
		{
			mStaffProxy = new StructStaffProxy( mStruct );
			mGroupProxy = new StructGroupProxy( mStruct );
			mTimesegProxy = new StructTimeProxy( mStruct );
			Downloader.downloadFiles( [DISK_DATA_URL], parseData );
		}
		
		private function parseData():void
		{
			/*******************************************
			               数据文件结构
			 <?xml?>
			 <!---请不要修改这个文件！！--->
			 <Database>
				<Staff>
					<item name="xxoo" identity="xxoo"/>
					...
					...
				</Staff>
				<Group>
					<item name="xxoo">1,2,3</item>
					...
					...
				</Group>
				<TimeSeg>
					<item name="xxoo" start="100" spend="120" size="6"/>
					...
					...
				</TimeSeg>
			 </Database>
			 *******************************************/
			 mStruct.loadFromXML( Pool.DIC_XML[DISK_DATA_FILE].old_xml );
			 mStaffProxy.init();
			 mGroupProxy.init();
			 mTimesegProxy.init();
		}
		
		public function get isDirty():Boolean
		{
			return mStaffProxy.isDirty || mGroupProxy.isDirty || mTimesegProxy.isDirty;
		}
		
		public function saveToDisk():void
		{
			if ( !isDirty ) return;
			
			saveFile( DISK_DATA_URL, mStruct.toXML() );
			
			mStaffProxy.clearDirty();
			mGroupProxy.clearDirty();
			mTimesegProxy.clearDirty();
		}
		
		public function queryStaffInterf():IDataProxy
		{
			return mStaffProxy;
		}
		public function queryGroupInterf():IDataProxy
		{
			return mGroupProxy;
		}
		public function queryTimesegInterf():IDataProxy
		{
			return mTimesegProxy;
		}
	}

}